package com.amigo.online.provider.common.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.amigo.online.common.base.controller.BaseController;
import com.amigo.online.common.core.response.Response;
import com.amigo.online.common.core.response.StatusCode;
import com.amigo.online.common.param.dto.user.UserAraeDto;
import com.amigo.online.common.param.dto.video.AreaDto;
import com.amigo.online.common.param.result.AreaInfo;
import com.amigo.online.common.param.result.GlobalConfigInfo;
import com.amigo.online.common.param.result.UserAreaInfo;
import com.amigo.online.common.util.CommonValidator;
import com.amigo.online.provider.common.exception.CommonException;
import com.amigo.online.provider.common.service.AreaService;
import com.amigo.online.provider.common.service.BusiAreaService;
import com.amigo.online.provider.common.service.GlobalConfigService;

/**
 * common相关
 *
 * @author amigo
 */
@RestController
@RequestMapping(value = "/common", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class CommonController extends BaseController {

    @Resource
    private GlobalConfigService globalConfigService;

    @Resource
    private AreaService areaService;

    @Resource
    private BusiAreaService busiAreaService;

    @GetMapping(value = "/global/config")
    public ResponseEntity<Response> getGlobalConfigList() {
        List<GlobalConfigInfo> list = globalConfigService.getGlobalConfigList();
        return success(list);
    }

    @GetMapping(value = "/area/name/{lang}/{id}")
    public ResponseEntity<Response> getAreaName(@PathVariable String lang, @PathVariable Integer id) {
        AreaInfo area = areaService.getAreaByIdAndLang(id, lang);
        return success(area);
    }


    /**
     * 地址联动查询
     *
     * @param dto
     * @return
     */
    @PostMapping(value = "/linkageQuery")
    public ResponseEntity<Response> linkageQuery(@RequestBody AreaDto dto) {
        if (!CommonValidator.validate(dto)) {
            throw new CommonException(StatusCode.BAD_REQUEST);
        }
        List<AreaInfo> areaInfoList = busiAreaService.linkageQuery(dto);
        return success(areaInfoList);
    }


    @GetMapping(value = "/global/config/md5")
    public ResponseEntity<Response> getGlobalMd5() {
        GlobalConfigInfo md5Info = globalConfigService.getGlobalMd5();
        return success(md5Info);
    }


    /**
     * 根据用户的信息查询用户地区
     */
    @PostMapping(value = "/userarea")
    public ResponseEntity<Response> getAreaInfo(@RequestBody UserAraeDto dto) {
        UserAreaInfo info = areaService.getAreaInfo(dto);
        return success(info);
    }
}
